package com.metis.document.parse.dialog.pipeline.impl;

import com.metis.document.parse.dialog.models.dto.AbstractElementDTO;
import com.metis.document.parse.dialog.models.dto.TableElementDTO;
import com.metis.document.parse.dialog.pipeline.ElementFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class NoUseTableFilter implements ElementFilter {
    /**
     * 阈值
     */
    private final double threshold = 0.05;
    @Override
    public boolean support(@NotNull AbstractElementDTO element) {
        return element instanceof TableElementDTO;
    }

    @Override
    public boolean needIgnore(@NotNull AbstractElementDTO element) {
        TableElementDTO table = (TableElementDTO) element;
        int count = table.getCells().size();
        double emptyCell = table.getCells().
                stream().filter(cell -> StringUtils.isBlank(cell.getTextContent()))
                .count();
        if (emptyCell/count > 1-threshold) {
            log.debug("ignore table: {}", table);
            return true;
        }
        return false;
    }
}
